import store from '../store';

/* 显示控件*/
export function showItem () {
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    if (isAndroid) {
        jsBridge.showItem()
    }
    if (isIos) {
        window.webkit.messageHandlers.showItem.postMessage('')
    }
}

/* 隐藏控件*/
export function hideItem () {
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    if (isAndroid) {
        jsBridge.hiddenItem()
    }
    if (isIos) {
        window.webkit.messageHandlers.hiddenItem.postMessage('')
    }
}

/* 获取token和ios版本*/
export function getToken () {
    let u = navigator.userAgent
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    if (isAndroid) {
        store.commit('setToken', jsBridge.getToken())
    }
    if (isIos) {
        window['getToken'] = (res) => {
            let resDate = res.split(',')
            store.commit('setToken', resDate[0])
            store.commit('setIosType', resDate[1])
        }
    }
}
